<?php echo HTML::contenido('plugins/daterangepicker/daterangepicker-bs3.css'); ?>
<?php echo HTML::contenido('plugins/daterangepicker/daterangepicker'); ?>
<?php echo HTML::script('jocnet/sortejos.js');?>
<div id="page-heading">
      <ol class="breadcrumb">
        <li><a href="<?php echo(url(''));?>">Portada</a></li>
        <li>Joc Net</li>
        <li class="active">Escollir guanyadors</li>
      </ol>
    </div>
    <div class="container">

<div class="row">
     <div class="col-md-12">
		<div class="box" style="border:none;">
	        <div class="box box-warning col-md-12" >
	        	<div class="box-header col-md-12">
									<i class="ion ion-clipboard"></i>
                                    <h3 class="box-title">Escollir Guanyadors Per Sorteig</h3>
				</div>
				<div class="box-body col-md-12">
					<dl class="dl-horizontal ">
				        <dt>Modificar nom</dt>
					        <dd>A description list is perfect for defining terms.</dd>
					    <dt>Eliminar</dt>
							<dd> id ligula porta felis euismod semper eget lacinia odio sem nec elit.</dd>
					</dl>
					<div class="col-md-4">
						Sorteig
						<select class="form-control" id="select_sorteig">
						       
						</select>
					</div>
					<div class="col-md-1">
						Ganadores
						<input type="text" maxlength="2" class="form-control col-md-1" id="nganadores"></input>
					</div>
					<div class="col-md-1">
						Reservas
						<input type="text" maxlength="2" class="form-control col-md-1" id="nreservas"></input>
					</div>
					<div class="col-md-1">
						<button style="margin-top:20px" onclick="escollir_guanyadors();" id="boton_select_ganadores" class="btn btn-primary">OK</button>
					</div>
				</div>			

		       <div class="row" id="tabla_ganadores"  style="display:none;">
                        <div class="col-md-12">
                            <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">Ganadores</h3>
                                    
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive no-padding">
                                    <table class="table table-hover" id="lista_ganadores">
                                        <tr>
                                           	<th>ID</th>
                                            <th>Codi</th>
                                            <th>Nom</th>
                                            <th>Cognoms</th>
                                            <th>Email</th>
                                            <th>Data Naixement</th>
                                            <th>Club</th>
                                            <th>Estat</th>
                                        </tr>
                                    </table>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div>
                    </div>
                    
                    
                    
                    <div class="row " id="tabla_reservas" style="display:none;">
                        <div class="col-md-12">
                            <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">Reservas</h3>
                                 
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive no-padding">
                                    <table class="table table-hover" id="lista_reservas">
                                        <tr>
                                            <th>ID</th>
                                            <th>Codi</th>
                                            <th>Nom</th>
                                            <th>Cognoms</th>
                                            <th>Email</th>
                                            <th>Data Naixement</th>
                                            <th>Club</th>
                                            <th>Estat</th>
                                        </tr>
                                        
                                     </table>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                        </div>
                    </div>
                    
                    
                    
                    
                    
                    
                    
			</div><!-- /.box-body -->
			<div class="box-footer clearfix no-border">
				<button class="btn btn-default pull-left"  onclick="confirmar_ganadores();" id="cambio_orden">
					<i class="fa fa-plus" ></i> Confirmar
				</button>
			</div>
		</div><!-- /.box -->
                            
     </div><!-- /.col -->

</div>

</div>



<script type="text/javascript">

	mostrar_sortejos_actius();

	function mostrar_sortejos_actius(){
		
		$.ajax({
		    url: url_base+"sortejos/sortejos_actius",
		    type: "POST",			      
			    beforeSend: function() {
			    fcf.cargando();
			}, 
			success: function(data){
					fcf.hidecargando();
					var data = jQuery.parseJSON(data);
					
					$.each( data, function( key, obj ) {
						 
						$("#select_sorteig").append('<option value="'+obj.id+'">'+obj.nom+'</option>');
					});
		 	}
		});
	}

	var lista_ganadores;
	var lista_reservas;

	function confirmar_ganadores(){

		var listado = '';
		
		$.each( lista_ganadores, function( key, obj ) {
			listado = listado + 'idusuario:'+obj.idusuari+',sorteig:'+obj.sorteig+',codi:'+obj.codi+',estado:ganador;';
		});

		if(jQuery.isEmptyObject($("#nreservas").val()) != true){ 
		
			$.each( lista_reservas, function( key, obj ) {
				listado = listado + 'idusuario:'+obj.idusuari+',sorteig:'+obj.sorteig+',codi:'+obj.codi+',estado:reserva;';
			});

		}

		$.ajax({
		    url: url_base+"sortejos/insert_ganadores_sorteo",
		    data: "listado="+listado,
		    type: "POST",			      
			    beforeSend: function() {
			    fcf.cargando();
			}, 
			success: function(data){
					fcf.hidecargando();
					alert(data);					
		 	}
		}); 
		
		
	}


	function escollir_guanyadors(){

		var id_sorteig = $("#select_sorteig").val();
		var nganadores = $("#nganadores").val();
		var nreservas = $("#nreservas").val();
		
		$.ajax({
		    url: url_base+"sortejos/get_guanyadors_per_sorteig",
		    data: "id_sorteig="+id_sorteig+"&nganadores="+nganadores+"&nreservas="+nreservas,
		    type: "POST",			      
			    beforeSend: function() {
			    fcf.cargando();
			}, 
			success: function(data){
					fcf.hidecargando();
					var json = jQuery.parseJSON(data);

					lista_ganadores = json.ganadores;
					lista_reservas = json.reservas;
					
					$.each( lista_ganadores, function( key, obj ) {
						$("#lista_ganadores").append('<tr><td>'+obj.id+'</td><td>'+obj.codi+'</td><td>'+obj.nom+'</td><td>'+obj.cognoms+'</td><td>'+obj.mail+'</td><th>'+obj.data_naixement+'</th><th>'+obj.club+'</th><th><span class="label label-success">Guanyador</span></th></tr>');

					});
					
					if(jQuery.isEmptyObject(nreservas) != true){ 
					
						$.each( lista_reservas, function( key, obj ) {
					     		$("#lista_reservas").append('<tr><td>'+obj.id+'</td><td>'+obj.codi+'</td><td>'+obj.nom+'</td><td>'+obj.cognoms+'</td><td>'+obj.mail+'</td><th>'+obj.data_naixement+'</th><th>'+obj.club+'</th><th><span class="label label-warning">Reserva</span></th></tr>');
						});
					
					}
										
					

					$("#tabla_ganadores").show();
					$("#tabla_reservas").show();
					$("#boton_select_ganadores").hide();
		 	}
		});

		

		
	}
	 
</script>

